/**
 * 作者: sessionliang
 * 时间: 2017-08-20
 * 描述: 预约考试详情页面
 */
define(function(require, exports, module) {
	"use strict";
	//每一个页面都要引入的工具类
	var CommonTools = require('CommonTools_Core');
	var WindowTools = require('WindowTools_Core');
	//轮播图工具类
	var GallerySliderTools = require('GallerySliderTools_Core');

	//base service
	var baseService = require('Base_Service');
	var exambookingService = require('ExamBooking_Service');
	var enumHelper = require('Common_Enum');
	var systemService = require("System_Service");
	var studyLogService = require("StudyLogs_Service");
	
	var km = null;
	var examid = null;
	var userInfo = null;
	//每一个页面都要引入的工具类
	// initready 要在所有变量初始化做完毕后
	CommonTools.initReady(initData);
	/**
	 * @description 初始化数据,结合initReady使用
	 * plus情况为plusready
	 * 其它情况为直接初始化
	 */
	function initData() {
		//引入必备文件,下拉刷新依赖于mui与mustache
		CommonTools.importFile([
			'js/libs/mui.min.js'
		], function() {
			userInfo = baseService.getUser();
			initHomePage();
		});
	}

	

	/**
	 * @description 渲染頁面
	 */
	function initHomePage() {
		km = WindowTools.getExtraDataByKey('km');
		examid = WindowTools.getExtraDataByKey('examid');
		initPlace();
		initListener();
	}
	function initListener(){
		mui("#exam_btnSubmit")[0].addEventListener("tap",function(){
			var data = {
				PlaceID: mui("#exam_ddlPlace")[0].value,
				CoachID: mui("#exam_ddlCoach")[0].value,
				ExamBookingID: examid
			};
			exambookingService.retry(data, function(retval){
				debugger;
				mui.toast('补考已提交,请到补考订单缴费');
				WindowTools.createWin(null, "../index.html",null, {});
			});
		});
	}
	
	function initPlace(){
		mui("#exam_ddlPlace")[0].innerHTML = "";
		
		systemService.getPlaces(userInfo.DrivingID,function(retval){
			var optionStr = "";
			if(retval){
				for(var index in retval){
					
					var item = retval[index];
					optionStr += "<option value='"+item.id+"'>"+item.name+"</option>";
				}
				mui("#exam_ddlPlace")[0].innerHTML = optionStr;
				initCoach();
			}
		});
	}
	
	/**
	 *@description 初始化教练 
	 */
	function initCoach(){
		mui("#exam_ddlCoach")[0].innerHTML = "";
		studyLogService.getCoachs(0, mui("#exam_ddlPlace")[0].value, km,true, function(retval){
			var optionStr = "";
			if(retval){
				for(var index in retval){
					var item = retval[index];
					optionStr += "<option value='"+item.CoachID+"'>"+item.Name+"</option>";
				}
				mui("#exam_ddlCoach")[0].innerHTML = optionStr;
			}
		});
	}
	
});